package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProHouse;
import com.pro_test.proservice.entity.ProOwner;
import com.pro_test.proservice.entity.vo.HouseQuery;
import com.pro_test.proservice.service.ProHouseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-house")
@CrossOrigin
public class ProHouseController {
    @Autowired
    private ProHouseService proHouseService;


    //查询房屋表所有数据
    @ApiOperation(value = "查询所有房屋")
    @GetMapping("findAll")
    public R findAllHouse()
    {
        List<ProHouse> list = proHouseService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除房屋
    @ApiOperation(value = "逻辑删除房屋")
    @DeleteMapping("{id}")
    public R removeHouse(@ApiParam(name = "id", value = "房屋ID", required = true)
                           @PathVariable String id)
    {
        boolean flag = proHouseService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageHouseCondition/{current}/{limit}")
    public R pageHouseCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HouseQuery houseQuery)
    {
        //创建page对象
        Page<ProHouse> pageHouse = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProHouse> wrapper = new QueryWrapper<>();
        String num = houseQuery.getNum();
        Integer status = houseQuery.getStatus();
        String begin = houseQuery.getBegin();
        String end = houseQuery.getEnd();

        if (!StringUtils.isEmpty(num))
        {
            wrapper.like("num",num);
        }
        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proHouseService.page(pageHouse,wrapper);
        long total = pageHouse.getTotal();
        List<ProHouse> records = pageHouse.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    //添加房屋
    @ApiOperation(value = "添加房屋")
    @PostMapping("addHouse")
    public R addHouse(@RequestBody ProHouse proHouse)
    {
        boolean save = proHouseService.save(proHouse);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "查询房屋")
    @GetMapping("getHouse/{id}")
    public R getHouse(@PathVariable String id)
    {
        ProHouse proHouse = proHouseService.getById(id);
        List<ProHouse> records = new ArrayList<>();
        records.add(proHouse);
        return R.ok().data("house",proHouse).data("proHouse",records);
    }



    //修改房屋
    @ApiOperation(value = "修改房屋")
    @PostMapping("updateHouse")
    public R updateHouse(@RequestBody ProHouse proHouse)
    {
        boolean flag = proHouseService.updateById(proHouse);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

